package com.huluspace.springtransactional.repository;

import com.huluspace.springtransactional.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository // 可省略，Spring Data会自动识别为Repository Bean
public interface UserRepository extends JpaRepository<User, Long> {


  // 用于联表查询加载角色
  @Query("SELECT u FROM User u LEFT JOIN FETCH u.roles WHERE u.username = :username")
  Optional<User> findByUsernameWithRoles(@Param("username") String username);

  // Spring Data JPA会自动解析方法名并生成查询
  Optional<User> findByUsername(String username);
}
